package com.morequick.controller.admin;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.morequick.model.User;
import com.morequick.service.UserService;
import com.morequick.util.CryptographyUtil;


@Controller
@RequestMapping("/admin")
public class IndexControler {
	
	@Resource
	private UserService userService;
	
	
	@RequestMapping("/login")
	public String login(){
		
		return "login";
	}
	
	@RequestMapping("/dologin")
	public String doLogin(User user,HttpServletRequest request){
		
		Subject subject=SecurityUtils.getSubject();
		UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(), CryptographyUtil.md5(user.getPassword(), "java1234"));
	
		try {
			subject.login(token);
			return "redirect:/admin/index.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("blogger", user);
			request.setAttribute("errorInfo", "用户名或密码错误！");
			return "login";
		}

		
	}
	
}
